<template>
  <div class="favorites-container">
    <button @click="goBack" class="back-button">返回</button>
    <h2 class="page-title">我的汽车</h2>

    <!-- 汽车列表展示 -->
    <div class="favorites-list">
      <div
          v-for="(item, index) in CarInfo"
          :key="index"
          class="favorite-item"
      >
        <div class="car-image">
          <img :src="item.avatarImg" alt="Car Image" class="car-img">
        </div>
        <div class="car-info">
          <h3>{{ item.carNo }}</h3>
          <p>品牌: {{ item.brand }}</p>
          <p>燃料: {{ item.fuelType }}</p>
          <p>排量: {{ item.displacement }}</p>
        </div>
        <div class="actions">
          <button @click="showDetails(item.id)" class="details-btn">
            <span class="material-icons">详情</span>
          </button>
        </div>
      </div>
    </div>
    <button class="add-btn" @click="addCar">新增</button>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import router from '@/router';
import { myCarList } from '@/api/myCar';

const CarInfo = ref();

function getCarInfo() {
  myCarList().then(res => {
    if (res.data.code == 200000){
      console.log(res.data)
      CarInfo.value = res.data.data;
    }else {
      alert(res.data.msg);
    }
  });
}
getCarInfo();



function goBack() {
  router.push("/mine/index");
}


function showDetails(id) {
  router.push(`/my/myCarDetails/${id}`);
}
function addCar() {
  router.push("/my/carAdd");
}
</script>
<style scoped>
/* 页面的整体布局 */
.favorites-container {
  padding: 20px;
  font-family: 'Arial', sans-serif;
  background-color: #f5f5f5;
  min-height: 100vh;
}

/* 页面标题样式 */
.page-title {
  font-size: 28px;
  color: #333;
  margin-bottom: 30px;
  text-align: center;
}

/* 返回按钮样式 */
.back-button {
  background-color: #4CAF50;
  color: white;
  padding: 10px 20px;
  border: none;
  cursor: pointer;
  border-radius: 4px;
  position: absolute;
  top: 20px;
  left: 20px;
  transition: background-color 0.3s ease;
}

.back-button:hover {
  background-color: #45a049;
}

/* 收藏列表容器 */
.favorites-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

/* 每个收藏项的卡片 */
.favorite-item {
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  transition: transform 0.2s ease-in-out;
}

.favorite-item:hover {
  transform: scale(1.03);
  box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
}

/* 收藏项图片 */
.car-image {
  width: 100px;
  height: 100px;
  overflow: hidden;
  border-radius: 5px;
}

.car-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 收藏项信息 */
.car-info {
  flex: 1;
  padding-left: 20px;
}

.car-info h3 {
  margin: 0;
  font-size: 20px;
  color: #333;
  font-weight: bold;
}

.car-info p {
  margin: 5px 0 0;
  font-size: 14px;
  color: #777;
}

/* 动作按钮 */
.actions {
  display: flex;
  gap: 10px;
}

.details-btn,
.remove-btn {
  background-color: #ff6f61;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s ease, transform 0.3s ease;
}

.details-btn:hover,
.remove-btn:hover {
  background-color: #f44336;
  transform: scale(1.05);
}

.details-btn .material-icons,
.remove-btn .material-icons {
  font-size: 18px;
}

/* 新增按钮 */
.add-btn {
  background-color: #2196F3;
  color: white;
  padding: 10px 20px;
  border: none;
  cursor: pointer;
  border-radius: 5px;
  margin-top: 20px;
  width: 100%;
  transition: background-color 0.3s ease;
}

.add-btn:hover {
  background-color: #1E88E5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .favorites-container {
    padding: 10px;
  }

  .page-title {
    font-size: 24px;
  }

  .favorites-list {
    grid-template-columns: 1fr;
  }

  .back-button {
    padding: 8px 16px;
    top: 10px;
    left: 10px;
  }
}
</style>